﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using websoniBO;

namespace websoni.Adminpages
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        Gallery g = new Gallery();
        Album al = new Album();
        
        protected void Page_Load(object sender, EventArgs e)
        {
            
            if (!IsPostBack)
            {
                btnupdate.Visible = false;
                //DataTable dt1 = al.GetRecords(-99, "", "", false, " and ");
                DataTable dt1 = al.GetRecords(-99, "", "", false, " and ");
                DataTable dt = g.GetRecords(-99, -99, "", "", false, " and ");
                ddlalbum.DataSource = dt1;
                ddlalbum.DataTextField = "album_name";
                ddlalbum.DataValueField = "alid";
                ddlalbum.DataBind();
                ddlalbum.Items.Insert(0, new System.Web.UI.WebControls.ListItem("---SELECT---", "-99"));

                

                //DataList1.DataSource = dt;
                //DataList1.DataBind();
                ddalbum2.DataSource = dt1;
                ddalbum2.DataTextField = "album_name";
                ddalbum2.DataValueField = "alid";
                ddalbum2.DataBind();
                ddalbum2.Items.Insert(0, new System.Web.UI.WebControls.ListItem("---SELECT---", "-99"));
                //Label4.Text = dt1.Rows[0]["album_name"].ToString();
                //a = Convert.ToInt64(dt1.Rows[0]["alid"].ToString());

            }
            
        }
        protected void btnok_Click(object sender, EventArgs e)
        {
            g.gid = -99;
            g.alid =Convert.ToInt64(ddlalbum.SelectedValue.ToString());
            if (uplodimage.HasFile)
                uplodimage.PostedFile.SaveAs(Server.MapPath("~/images/" + uplodimage.FileName));
            g.images = uplodimage.FileName;
            g.image_description = tbdescription.Text;
            g.isdeleted = false;
            g.SaveRecords();
           
            Response.Redirect("~/Adminpages/AGallery.aspx");
        }

        protected void btnupdate_Click(object sender, EventArgs e)
        {
            g.gid = Convert.ToInt64(tbgid.Text);
            //if (ddlalbum.SelectedValue == tbalid.Text)
                g.alid = Convert.ToInt64(tbalid.Text);
            //g.alid = Convert.ToInt64(ddlalbum.SelectedValue.ToString());
                if (uplodimage.HasFile)
                {
                    uplodimage.PostedFile.SaveAs(Server.MapPath("~/images/" + uplodimage.FileName));
                    g.images = uplodimage.FileName;
                }
                else
                {
                    g.images = tbimage.Text;
                }
            g.image_description = tbdescription.Text;
            g.isdeleted = false;
            g.SaveRecords();
            Response.Redirect("~/Adminpages/AGallery.aspx");
        }

        protected void btnedit_Command(object sender, CommandEventArgs e)
        {
            btnok.Visible = false; btnupdate.Visible = true;
            DataTable dte = g.GetRecords(Convert.ToInt64(e.CommandArgument),-99, "", "", false, " and ");
            tbgid.Text = dte.Rows[0]["gid"].ToString();
            tbalid.Text = dte.Rows[0]["alid"].ToString();
            ddlalbum.SelectedValue = dte.Rows[0]["alid"].ToString();
            tbimage.Text = dte.Rows[0]["images"].ToString();
            tbdescription.Text = dte.Rows[0]["image_description"].ToString();
        }

        protected void btndelete_Command(object sender, CommandEventArgs e)
        {
            DataTable dtd = g.GetRecords(Convert.ToInt64(e.CommandArgument), -99, "", "", false, " and ");
            g.gid = Convert.ToInt64(dtd.Rows[0]["gid"].ToString());
            g.alid = Convert.ToInt64(dtd.Rows[0]["alid"].ToString());
            g.images = dtd.Rows[0]["images"].ToString();
            g.image_description = dtd.Rows[0]["image_description"].ToString();
            g.isdeleted = true;
            g.SaveRecords();
            Response.Redirect("~/Adminpages/AGallery.aspx");
        }

        protected void btnview_Click(object sender, EventArgs e)
        {
            DataTable dt = g.GetRecords(-99,Convert.ToInt64(ddalbum2.SelectedValue.ToString()), "", "", false, " and ");
            DataList1.DataSource = dt;
            DataList1.DataBind();
        }
    }
}
